/**
 * 作者: wgx
 * 日期: 2024-11-13 16:41:36
 * 备注: 派对聊天记录表
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBChatMsg;
import com.motu.monstercity.server.game.base.AllParam;
import com.motu.monstercity.server.game.logic.ChatManager;
import com.motu.monstercity.server.game.logic.FeastManager;
import com.motu.vertx.module.utility.model.EntityManager;
import com.motu.vertx.module.utility.redis.RedisTools;
import com.motu.vertx.module.utility.toolset.TimeUtils;
import io.vertx.core.Future;
import io.vertx.core.json.JsonObject;

public class FeastChatRecord extends FeastChatRecordModel {
    public FeastChatRecord() {
        super();
    }

    public FeastChatRecord(long feastId, UserInfo userInfo, String msg) {
        super();
        putFeastId(feastId);
        putUserId(userInfo.getId());
        putUserName(userInfo.getUserName());
        putVip(userInfo.getVip());
        putContent(msg);
        putAddTime(TimeUtils.getCurTime());
    }

    public void doCreate() {
        setTableId();
        insert();
    }

    public JsonObject insert() {
        // 如果超过上限则会删除最早的那条
        long count = Future.await(RedisTools.getHashLen(getListKey()));
        if (count >= AllParam.CHAT_RECORD_MAX_NUM) {
            String orderListKey = EntityManager.getOrderListKey(getListKey());
            String delRecord = Future.await(RedisTools.lpop(orderListKey));// 删除列表数据
            long id = Long.parseLong(delRecord);// dbid
            FeastChatRecord feastChatRecord = FeastManager.getFeastChatRecord(getFeastId(), id);
            if (feastChatRecord != null) {
                feastChatRecord.delete();
            }
        }
        return super.insert();
    }

    public  PBChatMsg.Builder takeBuilder() {
        return ChatManager.takeChatMsgBuilder(getUserId(),getId(),ChatManager.CHAT_TYPE_FEAST,ChatManager.CHAT_SUB_TYPE_COMMON,getContent(),getAddTime(),"");

    }
}
